package adfc.game;

import adfc.utils.Perlin;

import static adfc.game.GameConstants.CHUNK_XY_SIZE;
import static adfc.game.GameConstants.CHUNK_Z_SIZE;

/**
 * Created by IntelliJ IDEA.
 * User: aimozg
 * Date: 31.12.11
 * Time: 17:52
 */
public class WorldGen {
    public WorldGen() {

    }

    private double[] genHeightmap(int gx, int gy) {
        double[] hmap = new double[CHUNK_XY_SIZE * CHUNK_XY_SIZE];
        Perlin perlin = new Perlin(0.5, 4);
        for (int y = 0; y < CHUNK_XY_SIZE; y++) {
            for (int x = 0; x < CHUNK_XY_SIZE; x++) {
                hmap[Chunk.xyoffset(x, y)] = perlin.noise2(x + gx * CHUNK_XY_SIZE, y + gy * CHUNK_XY_SIZE) * CHUNK_Z_SIZE;
            }
        }
        return hmap;
    }

    public void fillChunkBlocks(Chunk chunk, int gx, int gy) {
        double[] heightmap = genHeightmap(gx, gy);
        for (int y = 0; y < CHUNK_XY_SIZE; y++) {
            for (int x = 0; x < CHUNK_XY_SIZE; x++) {
                for (int z = 0; z < Math.min(CHUNK_Z_SIZE, heightmap[Chunk.xyoffset(x, y)]); z++) {
                    chunk.raw_blocks.put(Chunk.offset(x, y, z), Block.BID_STONE);
                }
            }
        }
        chunk.blocksUpdated();
    }
}
